package com.ghh.dao;

import com.ghh.pojo.CheckGroup;
import com.ghh.pojo.CheckItem;
import com.ghh.pojo.Setmeal;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface SetmealDao {

    //添加套餐，返回id
    @Insert("insert into t_setmeal values (#{id},#{name},#{code},#{helpCode},#{sex},#{age},#{price},#{remark},#{attention},#{img})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", resultType = Integer.class, keyColumn = "id")
    void addSetmeal(Setmeal setmeal);

    //将新增的t套餐id与检查组id添加到关联表中
    @Insert("insert into t_setmeal_checkgroup values (#{setmeal_Id},#{checkgroup_Id})")
    void addSetmealAndCheckGroup(@Param("setmeal_Id") Integer setmeal_Id, @Param("checkgroup_Id") Integer checkgroup_Id);

    //分页查询套餐
    @Select("<script>select * from t_setmeal <if test=\"value!=null and value!=''\">where code=#{value} or name=#{value} or helpCode=#{value}</if></script>")
    Page<Setmeal> findByCondition(@Param("value")  String queryString);

    //查询所有套餐图片的名字
    @Select("select img from t_setmeal")
    Set<String> findAllFileName();

    //查询所有的套餐信息
    @Select("select * from t_setmeal")
    List<Setmeal> findAllSetmeal();


    //查询所有套餐及检查项与检查组的详情
    @Select("select * from t_setmeal where id=#{id}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "checkGroups", column = "id",
                    many = @Many(select = "com.ghh.dao.CheckGroupDao.findAllCheckGroup"))
    })
    Setmeal findById(Integer id);

    //根据id查套餐基本信息
    @Select("select * from t_setmeal where id=#{id}")
    Setmeal findSetmealById(Integer id);

    @Select("select s.name name,count(o.id) value from t_order o,t_setmeal s where o.setmeal_id = s.id group by s.name")
    List<Map<String, Object>> findSetmealCount();
}
